﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Pixer 
{
    class Curve : Shape
    {
        private List<Point> points;

        public List<Point> Points
        {
            get { return points; }
            set { points = value; }
        }

        public Curve()
        {
            points = new List<Point>();
        }


        public override void paint(Graphics g)
        {
            if (points.Count >= 3)
            {
                int x, y, k = 20;
                int n = this.points.Count - 1;
                double t, t1, t2, t3, a, ab, c, d;
                t = 1.0 / k;
                Point[] apoints = this.points.ToArray();
                apoints[0].X = 2 * apoints[1].X - apoints[2].X;
                apoints[0].Y = 2 * apoints[1].Y - apoints[2].Y;
                apoints[n].X = 2 * apoints[n - 1].X - apoints[n - 2].X;
                apoints[n].Y = 2 * apoints[n - 1].Y - apoints[n - 2].Y;
                Line line = new Line();
                line.LineColor = lineColor;
                line.StartPoint = new Point(apoints[1].X, apoints[1].Y);
                for (int i = 1; i < n - 1; i++)
                    for (int j = 1; j <= k; j++)
                    {
                        t1 = j * t;
                        t2 = t1 * t1;
                        t3 = t2 * t1;
                        a = (3 * t2 - t3 - 3 * t1 + 1) / 6;
                        ab = (3 * t3 - 6 * t2 + 4) / 6;
                        c = (3 * t2 - 3 * t3 + 3 * t1 + 1) / 6;
                        d = t3 / 6;
                        x = (int)(a * apoints[i - 1].X + ab * points[i].X + c * apoints[i + 1].X + d * apoints[i + 2].X);
                        y = (int)(a * apoints[i - 1].Y + ab * apoints[i].Y + c * apoints[i + 1].Y + d * apoints[i + 2].Y);
                        line.EndPoint = new Point(x, y);

                        line.paint(g);

                        line.StartPoint = line.EndPoint;

                    }
            }
            else if(points.Count >= 2)
            {
                Line l = new Line();
                Point[] ap = points.ToArray();
                l.StartPoint = ap[0];
                l.EndPoint = ap[1];
                l.LineColor = lineColor;
                l.paint(g);
            }

        }

        public override void paint(Bitmap b)
        {
            if (this.points.Count < 2) return;

            int x, y, k = 20;
            int n=this.points.Count-1;
            double t, t1, t2, t3, a, ab, c, d;
            t = 1.0 / k;
            Point[] points = this.points.ToArray();
            points[0].X = 2 * points[1].X - points[2].X;
            points[0].Y = 2 * points[1].Y - points[2].Y;
            points[n].X=2*points[n-1].X-points[n-2].X;
            points[n].Y = 2 * points[n - 1].Y - points[n - 2].Y;
            Line line = new Line();
            line.LineColor = lineColor;
            line.StartPoint=new Point(points[1].X,points[1].Y);
            for(int i=1;i<n-1;i++)
                for (int j = 1; j <= k; j++)
                {
                    t1 = j * t;
                    t2 = t1 * t1;
                    t3 = t2 * t1;
                    a = (3 * t2 - t3 - 3 * t1 + 1) / 6;
                    ab = (3 * t3 - 6 * t2 + 4) / 6;
                    c = (3 * t2 - 3 * t3 + 3 * t1 + 1) / 6;
                    d = t3 / 6;
                    x = (int)(a * points[i - 1].X + ab * points[i].X + c * points[i + 1].X + d * points[i + 2].X);
                    y = (int)(a * points[i - 1].Y + ab * points[i].Y + c * points[i + 1].Y + d * points[i + 2].Y);
                    line.EndPoint = new Point(x,y);
                    
                    line.paint(b);

                    line.StartPoint = line.EndPoint ;

                }
        }
    }
}
